package com.cby.android.olive.ext

import android.app.Activity
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.util.Pair
import android.view.View
import android.view.WindowManager
import com.cby.android.olive.R

/**
 * @description: ActivityExt
 *
 * @author ChenBaoyang
 * @date 2021/11/3 17:12
 */
/**
 * Activity 增加一个跳转的拓展函数
 * @receiver Activity
 * @param clazz Class<out Any>
 */
fun Activity.start(clazz: Class<out Any>) {
    startActivity(Intent(this, clazz))
}

/**
 * Activity 增加一个跳转的拓展函数,并且带参数[Bundle]
 * @receiver Activity
 * @param intent
 */
fun Activity.start(intent: Intent) {
    startActivity(intent)
}

/**
 * Activity 增加一个跳转的拓展函数,并且带参数[Bundle]
 * @receiver Activity
 * @param clazz Class<out Any>
 * @param bundle Bundle
 */
fun Activity.start(clazz: Class<out Any>, bundle: Bundle) {
    val intent = Intent(this, clazz)
    intent.putExtras(bundle)
    startActivity(intent)
}

/**
 * Activity 增加一个跳转的拓展函数
 * @receiver Activity
 * @param clazz Class<out Any>
 * @param pairs Array<out Pair<String, Any?>>
 * @param block [@kotlin.ExtensionFunctionType] Function1<Intent, Unit>
 */
fun Activity.start(
    clazz: Class<out Any>,
    vararg pairs: Pair<String, Any?>,
    block: Intent.() -> Unit = {}
) {
    val data = bundleOf(*pairs)
    val intent = Intent(this, clazz)
    intent.block()
    intent.putExtras(data)
    start(intent)
}



/**
 * Activity 增加一个跳转的拓展函数
 * @receiver Activity
 * @param pairs Array<out Pair<String, Any?>>
 * @param block [@kotlin.ExtensionFunctionType] Function1<Intent, Unit>
 */
fun Activity.start(
    vararg pairs: Pair<String, Any?>,
    block: Intent.() -> Unit = {}
) {
    val data = bundleOf(*pairs)
    val intent = Intent()
    intent.block()
    intent.putExtras(data)
    startActivity(intent)
}



fun Activity.finishWithResult(vararg pairs: Pair<String, Any?>) {
    setResult(Activity.RESULT_OK, Intent().apply { putExtras(bundleOf(*pairs)) })
    finish()
}

/**
 * Activity 增加一个获取view对象的拓展函数,并且带参数[resId]
 * @receiver Activity
 * @param resId Int
 * @return T
 */
fun <T : View> Activity.getView(resId: Int) :T{
    return findViewById(resId)
}

/**
 * 隐藏魅族、Nexus、华为等底部的虚拟导航按键，避免遮挡内容
 *
 * @param activity 需要隐藏底部导航按键的Activity
 */
fun Activity.hideBottomUIMenu() {
    //隐藏虚拟按键，并且全屏
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        val v = window.decorView
        v.systemUiVisibility = View.GONE
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        val decorView = window.decorView
        val uiOptions = (View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                or View.INVISIBLE
                or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
        decorView.systemUiVisibility = uiOptions
    }
}

/**
 * 设置activity全屏
 */
fun Activity.setFullScreen(){
    setTheme(R.style.CbyNoActionBar)
    window.setFlags(
        WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN
    )
    hideBottomUIMenu()

}